﻿using ImperialServer.Common.Entities;
using ImperialUtilities.Exceptions;
using ImperialUtilities.Utilities;
using ImperialWebSite.Content.Resources;
using ImperialWebSite.Controllers;
using ImperialWebSite.Presenters.Role.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ImperialWebSite.Presenters.Role.Classes
{
    public class RoleRegistryPresenter
    {
        private IRoleRegistry _view;
        private RoleController _controller;
        private EventViewer _logger;

        /// <summary>
        /// Contructor de la clase
        /// </summary>
        /// <param name="view">Interfaz que enlaza con la vista</param>
        public RoleRegistryPresenter(IRoleRegistry view)
        {
            _view = view;
            _controller = new RoleController();
            _logger = new EventViewer("ImperialWebSite.Presenters.Role.Classes");
        }

        /// <summary>
        /// Metodo que hace la carga de los roles existentes en la base de datos
        /// </summary>
        internal void LoadCurrentRoles()
        {
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "LoadCurrentRoles"));

                _view.SessionCurrentRoles = _controller.SelectAllRoles();

                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = ie.ImperialMessage;
            }
            catch (Exception e)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = Messages.failureTransactionMessage;
                _logger.WriteEventViewerLog("LoadCurrentRoles", e.Message, true);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "LoadCurrentRoles"));
            }
        }

        /// <summary>
        /// Metodo que realiza la insercion de un nuevo rol
        /// </summary>
        internal void InsertNewRole()
        {
            RoleEntity newRole = null;
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "InsertNewRole"));

                if (!_controller.RoleAlreadyRegistered(_view.RoleNameText, _view.SessionCurrentRoles))
                {
                    newRole = _controller.CreateNewRole(_view.RoleNameText);
                    if (_controller.InsertNewRole(newRole))
                    {
                        _view.SuccessfulTransaction = true;
                        _view.TransactionMessage = Messages.successfulTransactionMessage;
                    }
                }
                else
                {
                    _view.SuccessfulTransaction = false;
                    _view.TransactionMessage = Messages.duplicatedRoleMessage;
                }

                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = ie.ImperialMessage;
            }
            catch (Exception e)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = Messages.failureTransactionMessage;
                _logger.WriteEventViewerLog("InsertNewRole", e.Message, true);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "InsertNewRole"));
            }
        }
    }
}